module dgl.graphics.LoadHelper;

import std.string;
import std.file;
import std.conv;

import dgl.Vector2;
import dgl.Vector3;
import dgl.Vector4;
import dgl.Quaternion;

class LoadHelper
{
	private string[] m_lines;

	private uint m_curLine;

	public this( string filename )
	{
		m_lines = splitlines( cast(char[])read( filename ) );
		m_curLine = 0;
	}

	public string readLine( out string data )
	{
		string line;
		line = m_lines[m_curLine];
		int index = 0;
		do
		{
			index = find( line, " " );
		} while( index == -1 )
			
		data = line[index+1 .. line.length];
		m_curLine++;
		return line[0 .. index];
	}
	
	public static Vector2 stringsToVector2( string data )
	{
		string[] elements = split( data, " " );
		if( elements.length == 2 )
		{
			Vector2 ret;
			ret.x = toFloat( elements[0] );
			ret.y = toFloat( elements[1] );
			return ret;
		}
		return Vector2.zero;
	}
	
	public static Vector3 stringsToVector3( string data )
	{
		string[] elements = split( data, " " );
		if( elements.length == 3 )
		{
			Vector3 ret;
			ret.x = toFloat( elements[0] );
			ret.y = toFloat( elements[1] );
			ret.z = toFloat( elements[2] );
			return ret;
		}
		return Vector3.zero;
	}
	
	public static Vector4 stringsToVector4( string data )
	{
		string[] elements = split( data, " " );
		if( elements.length == 4 )
		{
			Vector4 ret;
			ret.x = toFloat( elements[0] );
			ret.y = toFloat( elements[1] );
			ret.z = toFloat( elements[2] );
			ret.w = toFloat( elements[3] );
			return ret;
		}
		return Vector4.zero;
	}
	
	public static Quaternion stringsToQuaternion( string data )
	{
		string[] elements = split( data, " " );
		if( elements.length == 4 )
		{
			Quaternion ret;
			ret.x = toFloat( elements[0] );
			ret.y = toFloat( elements[1] );
			ret.z = toFloat( elements[2] );
			ret.w = toFloat( elements[3] );
			return ret;
		}
		return Quaternion.identity;
	}
}